home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1998 April / MACPOWER-1998-04.ISO.7z / MACPOWER-1998-04.ISO / Shareware Paradise / Tabler.091.sit / Tabler 0.9.1 / source code / Convert.c next >
Text File  |  1996-11-02  |  6KB  |  223 lines

  1. /*
  2.  *--------------------------------------------------------------
  3.  * Convert.c
  4.  *--------------------------------------------------------------
  5.  *    the heart of Tabler
  6.  *--------------------------------------------------------------
  7.  */
  8.  
  9. /* System Include Headers */
  10. #include <Types.h>
  11. #include <Memory.h>
  12. #include <TextUtils.h>
  13. #include <Resources.h>
  14. #include <Files.h>
  15. #include <Folders.h>
  16. #include <Sound.h>
  17. #include <Errors.h>
  18.  
  19. /* Project Include Header */
  20. #include "Tabler.h"
  21.  
  22. /* Resource IDs */
  23. enum constSoudID {
  24.     kMySoundID    = 8192
  25. };
  26.  
  27. /* Global data for HTML tags */
  28. static unsigned char    gTab[] = {"¥p¥t"};
  29. static unsigned char    gComma1[] = {"¥p,"};
  30. static unsigned char    gComma2[] = {"¥p, "};
  31. static unsigned char    gReturn[] = {"¥p¥r"};
  32. static unsigned char    gTagMark[] = {"¥p<table "};
  33.  
  34. static char    tagTableBgn[] = {"<table border cellpadding cellspacing width>¥r<tr><td>"};
  35. static char    tagTableEnd[] = {"</table>¥r"};
  36. static char    tagTableClm[] = {"</td><td>"};
  37. static char    tagTableRow[] = {"</td></tr>¥r<tr><td>"};
  38. static char    tagTableOne[] = {"<tr><td>"};
  39.  
  40.  
  41. /* Static function prototypes */
  42. static OSErr MakeTableTags(Handle src, Handle dst);
  43. static short GetALine(Ptr *buff);
  44. static OSErr ConvertTags(Handle src, Handle dst);
  45. static OSErr Replace(Handle target, Ptr subst, short len, StringPtr key);
  46. static Boolean IsTableTag(Handle);
  47.  
  48. /* Implementation */
  49. /*
  50.  *--------------------------------------------------------------
  51.  * ConvertScrap
  52.  *--------------------------------------------------------------
  53.  *    get a text from scrap, if any, and make it to be converted
  54.  *--------------------------------------------------------------
  55.  */
  56. OSErr ConvertScrap(void)
  57. {
  58.     OSErr    result = noErr;
  59.     long    scrapOffset;
  60.     long    scrapCount = GetScrap(nil, 'TEXT', &scrapOffset);
  61.  
  62.     if (scrapCount > 0) {
  63.         /* do convert if TEXT exits */
  64.         Handle    srcHandle = NewHandle(0);
  65.         Handle    dstHandle = nil;
  66.         if (srcHandle != nil) {
  67.             /* really get the scrap data */
  68.             GetScrap(srcHandle, 'TEXT', &scrapOffset);
  69.             if (IsTableTag(srcHandle) == false) {
  70.                 dstHandle = NewHandle(0);
  71.                 if (dstHandle != nil) {
  72.                     /* then convert it */
  73.                     SoundMyClick();
  74.                     result = MakeTableTags(srcHandle, dstHandle);
  75.                     if (result == noErr) {
  76.                         /* put it into the scrap */
  77.                         ZeroScrap();
  78.                         HLock(dstHandle);
  79.                         result = PutScrap(GetHandleSize(dstHandle), 'TEXT', *dstHandle);
  80.                         HUnlock(dstHandle);
  81.                     }
  82.                 } else {
  83.                     result = MemError();
  84.                 }
  85.             }
  86.         } else {
  87.             result = MemError();
  88.         }
  89.         /* clean up */
  90.         if (srcHandle != nil) DisposeHandle(srcHandle);
  91.         if (dstHandle != nil) DisposeHandle(dstHandle);
  92.     }
  93.     return result;
  94. }
  95. /*
  96.  *--------------------------------------------------------------
  97.  * MakeTableTags
  98.  *--------------------------------------------------------------
  99.  *    convert from a given source data to a destination data
  100.  *--------------------------------------------------------------
  101.  */
  102. static OSErr MakeTableTags(Handle srcData, Handle dstData)
  103. {
  104.     OSErr    result = nilHandleErr;
  105.  
  106.     if ((srcData != nil) && (dstData != nil)) {
  107.  
  108.         Size    dataLen;
  109.         Handle    tmpData = NewHandle(0);
  110.  
  111.         if (tmpData != nil) {
  112.             result = HandAndHand(srcData, tmpData);
  113.         }
  114.         if (result == noErr) {
  115.             dataLen = (GetHandleSize(tmpData));
  116.             if ((*tmpData)[dataLen-1] != '¥r') {
  117.                 SetHandleSize(tmpData, dataLen+1);
  118.                 result = ResError();
  119.             }
  120.         }
  121.         if (result == noErr) {
  122.             (*tmpData)[dataLen] = '¥r';
  123.             result = Replace(tmpData, tagTableRow, sizeof(tagTableRow)-1, gReturn);
  124.         }
  125.         if (result >= noErr) {
  126.             /* try tab at first */
  127.             result = Replace(tmpData, tagTableClm, sizeof(tagTableClm)-1, gTab);
  128.             if (result == noErr) {
  129.                 /* then try "comma + space" */
  130.                 result = Replace(tmpData, tagTableClm, sizeof(tagTableClm)-1, gComma2);
  131.                 if (result == noErr) {
  132.                     /* try comma at last */
  133.                     result = Replace(tmpData, tagTableClm, sizeof(tagTableClm)-1, gComma1);
  134.                 }
  135.             }
  136.         }
  137.         if (result >= noErr) {
  138.             SetHandleSize(tmpData, GetHandleSize(tmpData) - (sizeof(tagTableOne)-1));
  139.             result = MemError();
  140.         }
  141.         if (result == noErr) {
  142.             result = PtrAndHand(tagTableBgn, dstData, sizeof(tagTableBgn)-1);
  143.         }
  144.         if (result == noErr) {
  145.             result = HandAndHand(tmpData, dstData);
  146.         }
  147.         if (result == noErr) {
  148.             result = PtrAndHand(tagTableEnd, dstData, sizeof(tagTableEnd)-1);
  149.         }
  150.         if (tmpData != nil) {
  151.             DisposeHandle(tmpData);
  152.         }
  153.     }
  154.     return result;
  155. }
  156. /*
  157.  *--------------------------------------------------------------
  158.  * Replace
  159.  *--------------------------------------------------------------
  160.  *    replace the given words using ReplaceText
  161.  *--------------------------------------------------------------
  162.  */
  163. static OSErr Replace(Handle targetText, Ptr substPtr, short length, StringPtr keyWord)
  164. {
  165.     Handle    substText;
  166.     short    result;
  167.  
  168.     result = PtrToHand(substPtr, &substText, length);
  169.     if (result == noErr) {
  170.         result = ReplaceText(targetText, substText, keyWord);
  171.         DisposeHandle(substText);
  172.     }
  173.     return (OSErr)result;
  174. }
  175. /*
  176.  *--------------------------------------------------------------
  177.  * IsTableTag
  178.  *--------------------------------------------------------------
  179.  *    check whether if the text is already HTML table tag
  180.  *--------------------------------------------------------------
  181.  */
  182. static Boolean IsTableTag(Handle theText)
  183. {
  184.     Str31    testStr;
  185.     Size    testSize = Length(gTagMark);
  186.     UInt8    hState = HGetState(theText);
  187.  
  188.     /* make a string from the text handle */
  189.     testStr[0] = testSize;
  190.     HLock(theText);
  191.     BlockMoveData(*theText, &testStr[1], testSize);
  192.     HSetState(theText, hState);
  193.  
  194.     return (EqualString(testStr, gTagMark, false, true));
  195. }
  196. /*
  197.  *--------------------------------------------------------------
  198.  * PlayAClick
  199.  *--------------------------------------------------------------
  200.  *    play a custom click sound
  201.  *--------------------------------------------------------------
  202.  */
  203. void PlayAClick(void)
  204. {
  205.     Handle    aSound;
  206.  
  207.     aSound = GetResource('snd ', kMySoundID);
  208.     if (aSound != nil) {
  209.         DetachResource(aSound);
  210.         SndPlay(nil, (SndListHandle)aSound, true);
  211.     }
  212. /*
  213.     The reason we have to detach the sound resource is that we have
  214.     to close the current resource fork immediately in case of FKEY.
  215.     As of Sound Manager 3.1, SndPlay play sound asyncronously
  216.     even if we set the flag to true. It will be changed, however,
  217.     in the future version of Sound Manager. Hence we don't dispose
  218.     the sound handle here. Don't forget to set the sound resource
  219.     unlocked and PURGEABLE.
  220. */
  221. }
  222. /* end of Convert.c */
  223.